package com.ems.controller.Invigilate.room;

import com.ems.pojo.Invigilate.room.RoomUser;
import com.ems.pojo.Invigilate.room.TestRoom;
import com.ems.pojo.data.RequestData;
import com.ems.pojo.data.ResultData;
import com.ems.pojo.data.RetunResult;
import com.ems.pojo.data.ReturnPageResult;
import com.ems.pojo.user.User;
import com.ems.service.Invigilate.room.RoomUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Auther: 伟伟
 * @Date: 2020/2/18
 * @Description: 描述
 * @Version: 1.0
 */
@Controller
@CrossOrigin //允许所有ip跨域
@RequestMapping("/roomUser")
public class RoomUserController {
    @Autowired
    @Qualifier("roomUserService")
    private RoomUserService roomUserService;

    //同意考生加入到指定考场
    @ResponseBody
    @RequestMapping("/allowUser")
    public ResultData allowUser(@RequestBody RoomUser roomUser){
        roomUserService.allowUser(roomUser);
        return RetunResult.AssemblyData();
    }


    //删除考生的申请信息
    @ResponseBody
    @RequestMapping("/deleteAllowUser")
    public ResultData deleteAllowUser(@RequestBody List<RoomUser> roomUsers){
        roomUserService.deleteAllowUser(roomUsers);
        return RetunResult.AssemblyData();
    }

    //获取该监考教师下面所有的考生信息
    @ResponseBody
    @RequestMapping("/getRoomUserInfo")
    public ResultData getRoomUserInfo(@RequestBody RequestData requestData, HttpServletRequest request){
        ReturnPageResult.setPageHelperInfo(requestData);
        User user = (User) request.getSession().getAttribute("user");
        List<User> list = roomUserService.getRoomUserInfo(requestData.getMap(),user);
        return ReturnPageResult.AssemblyData(list);
    }

    //监考删除已同意的考生
    @ResponseBody
    @RequestMapping("/deleteAlreadyUser")
    public ResultData deleteAlreadyUser(@RequestBody List<User> userList){
        roomUserService.deleteAlreadyUser(userList);
        return RetunResult.AssemblyData();
    }

    //学生查询要申请的考场信息
    @ResponseBody
    @RequestMapping("/getStudentApply")
    public ResultData getStudentApply(@RequestBody RequestData requestData,HttpServletRequest request){
        ReturnPageResult.setPageHelperInfo(requestData);
        User user = (User) request.getSession().getAttribute("user");
        List<RoomUser> list = roomUserService.getStudentApply(requestData.getMap(),user);
        return ReturnPageResult.AssemblyData(list);
    }

    //考生删除申请信息
    @ResponseBody
    @RequestMapping("/deleteApplyTestRoom")
    public ResultData deleteApplyTestRoom(@RequestBody List<RoomUser> list,HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        roomUserService.deleteApplyTestRoom(list,user);
        return RetunResult.AssemblyData();
    }

    //考生申请进入指定的考场
    @ResponseBody
    @RequestMapping("/agreeApplyTestRoom")
    public ResultData agreeApplyTestRoom(@RequestBody TestRoom testRoom,HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        int result = roomUserService.agreeApplyTestRoom(testRoom,user);
        if(result == -1) {
            return RetunResult.AssemblyData("考场不存在");
        }else{
            return RetunResult.AssemblyData();
        }
    }

    /**
     * 考生查询已进入的考场
     * @param requestData
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/getAlreadyTestRooInfo")
    public ResultData getAlreadyTestRooInfo(@RequestBody RequestData requestData,HttpServletRequest request){
        ReturnPageResult.setPageHelperInfo(requestData);
        User user = (User) request.getSession().getAttribute("user");
        List<RoomUser> list = roomUserService.getAlreadyTestRooInfo(requestData.getMap(),user);
        return ReturnPageResult.AssemblyData(list);
    }

    /**
     * 考生退出指定的考场
     * @param roomUserList
     * @return
     */
    @ResponseBody
    @RequestMapping("/exitAlreadyTestRoom")
    public ResultData exitAlreadyTestRoom(@RequestBody List<RoomUser> roomUserList){
        roomUserService.exitAlreadyTestRoom(roomUserList);
        return RetunResult.AssemblyData();
    }

}
